package com.projectm.project.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.framework.common.utils.ServletUtils;
import com.projectm.common.Constant;
import com.projectm.common.MenuUtils;
import com.projectm.member.domain.MemberAccount;
import com.projectm.member.mapper.MemberAccountMapper;
import com.projectm.project.domain.ProjectAuth;
import com.projectm.project.domain.ProjectMenu;
import com.projectm.project.mapper.ProjectAuthMapper;
import com.projectm.project.mapper.ProjectMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ProjectMenuService extends ServiceImpl<ProjectMenuMapper, ProjectMenu> {

    @Autowired
    private ProjectAuthMapper projectAuthMapper;
    @Autowired
    private MemberAccountMapper memberAccountMapper;

    // 系统角色获取菜单权限
    public List<ProjectMenu> getCurrentUserMenu(String code, String userCode){
        boolean isRoot = true;
        // 查该组织的角色
        QueryWrapper<MemberAccount> wrapper = new QueryWrapper<>();
        wrapper.eq("organization_code", code).
                eq("member_code", userCode);
        Integer auth = Integer.valueOf(memberAccountMapper.selectOne(wrapper).getAuthorize());
        // 查是不是管理员
        ProjectAuth projectAuth = projectAuthMapper.selectById(auth);
        if (projectAuth!=null) {
            if ("member".equals(projectAuth.getType())) {
                isRoot = false;
            }
        }
        List<ProjectMenu> lstMenu = baseMapper.selectAllProjectMenu(new HashMap(){{
            put("status",1);
        }});
        MenuUtils mu = new MenuUtils(lstMenu);
        List<ProjectMenu> projectMenus = mu.builTree();
        if (!isRoot) {
            // 不是管理员 去除系统管理功能菜单
            projectMenus.remove(3);
        }
        return projectMenus;
    }
    public List<ProjectMenu> getCurrentUserMenu(){
        List<ProjectMenu> lstMenu = baseMapper.selectAllProjectMenu(new HashMap(){{put("status",1);}});
        MenuUtils mu = new MenuUtils(lstMenu);
        return mu.builTree();
    }
    public List<ProjectMenu> getAllProjectMenuTree(){
        List<ProjectMenu> listMenu = baseMapper.selectAllProjectMenu(new HashMap());
        MenuUtils mu = new MenuUtils(listMenu);
        return mu.builTree();
    }

    public List<ProjectMenu> getProjectMenuByStatus(String status){
        List<ProjectMenu> listMenu = baseMapper.selectAllProjectMenu(new HashMap(){{put("status",status);}});
        MenuUtils mu = new MenuUtils(listMenu);
        return mu.builTree();
    }
    /**
     * 获取子目录的ID，并删除（问题）
     * @param id
     * @return
     */
    @Transactional
    public Integer menuDelete(Integer id){
        List<Integer> ids = new ArrayList<>();
        ids.add(id);
        List<ProjectMenu> childMenu = baseMapper.selectProjectMenuByPid(new HashMap(){{put("pid",id);}});
        List<ProjectMenu> pMenuList = baseMapper.selectAllProjectMenu(new HashMap());
        for(ProjectMenu m:childMenu){
            ids.add(m.getId());
            ids.addAll(MenuUtils.getMenuIds(pMenuList,m));
        }
        return baseMapper.deleteBatchIds(ids);
    }

}
